package adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.boltshow.BrandDetailActivity;
import com.example.administrator.boltshow.R;
import com.squareup.picasso.Picasso;

import java.util.List;

import model.AllPraiseModel;

/**
 * Created by lenovo on 2016/8/29.
 */
public class AllPraiseAdapter extends RecyclerView.Adapter<AllPraiseAdapter.MyViewHolder> {
    private Context mContext;
    private List<AllPraiseModel.BrandsBean> mData;
    private View.OnClickListener mClickListener;


    public AllPraiseAdapter(Context mContext, List<AllPraiseModel.BrandsBean> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_praise_show,null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
      //  Log.i("TAG", "mData.get(position).getBrandIcon()"+mData.get(position).getBrandIcon());

       Picasso.with(mContext).load(mData.get(position).getBrandIcon()).into(holder.imageView);
        String isDown = mData.get(position).getIsDown();
        if (isDown.equals("0")){
            holder.tvBottom.setVisibility(View.VISIBLE);
            holder.btBottom.setVisibility(View.VISIBLE);
            holder.tvBottom.setVisibility(View.INVISIBLE);
            holder.btBottom.setTag(position);
            holder.btBottom.setOnClickListener(mClickListener);

            holder.mClick.setTag(position);
            holder.mClick.setOnClickListener(mClickListener);
        }else if(isDown.equals("1")) {
            holder.btBottom.setVisibility(View.VISIBLE);
            holder.btBottom.setVisibility(View.INVISIBLE);
            holder.tvBottom.setVisibility(View.VISIBLE);
        }
        final String brandId = mData.get(position).getBrandId();
        final String brandName = mData.get(position).getBrandName();
     /*   RotateAnimation anim = new RotateAnimation(0, -150, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                0.5f);
        anim.setDuration(1000);
        anim.setRepeatCount(1);*/


    //    anim.setRepeatMode(Animation.REVERSE);
    //    holder.imageView.setAnimation(anim);

        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent =new Intent(mContext,BrandDetailActivity.class);
                intent.putExtra("brandId",brandId);
                intent.putExtra("brandName",brandName);
                mContext.startActivity(intent);
            }
        });
        holder.tvBottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(mContext,BrandDetailActivity.class);
                intent.putExtra("brandId",brandId);
                intent.putExtra("brandName",brandName);
                mContext.startActivity(intent);
            }
        });

        holder.btBottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(mContext,BrandDetailActivity.class);
                intent.putExtra("brandId",brandId);
                intent.putExtra("brandName",brandName);
                mContext.startActivity(intent);
            }
        });


    }

    @Override
    public int getItemCount() {
        return mData==null?0:mData.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;
        private Button btBottom;
        private TextView tvBottom;
        private CardView mClick;

        public MyViewHolder(View itemView) {

            super(itemView);

            imageView= (ImageView) itemView.findViewById(R.id.iv_allpraise_bg);
            btBottom= (Button) itemView.findViewById(R.id.bt_allpriase_dianzan);
            tvBottom= (TextView) itemView.findViewById(R.id.tv_allpraise_bottom);
            mClick= (CardView) itemView.findViewById(R.id.fdssdsdsadsadsaadsa);
        }
    }

    public  void setOnItemClickListener(View.OnClickListener mClickListener){
        this.mClickListener=mClickListener;
    }

    public void setOnClickListener(View.OnClickListener OnClickListener){
        mClickListener=OnClickListener;
    }
}
